<template>
    <div>
        <h1>添加</h1>
        <table>
            <tbody>
                <tr>
                    <td>用户姓名</td>
                    <td>
                        <input type="text" v-model="data.name">
                    </td>
                </tr>
                <tr>
                    <td>性别</td>
                    <td>
                        <input type="radio" value="true" name="sex" v-model="data.sex">男
                        <input type="radio" value="false" name="sex" v-model="data.sex" checked>女
                    </td>
                </tr>
                <tr>
                    <td>手机号</td>
                    <td>
                        <input type="text" v-model="data.phone">
                    </td>
                </tr>
                <tr>
                    <td>会员等级</td>
                    <td>
                        <select v-model="data.memberClassId">
                            <option value="0">请选择</option>
                            <option v-for="a in xiala" :value="a.memberClassId">{{ a.memberName }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>消费金额</td>
                    <td>
                        <input type="text" v-model="data.price">
                    </td>
                </tr>
                <tr>
                    <td>消费次数</td>
                    <td>
                        <input type="text" v-model="data.prices">
                    </td>
                </tr>
                <tr>
                    <td>可用积分</td>
                    <td><input type="text" v-model="data.initd"></td>
                </tr>
                <tr>
                    <td>可用余额</td>
                    <td><input type="text" v-model="data.initds"></td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td>
                        <input type="checkbox" v-model="data.indele">勾选为true
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="button" value="新增" @click="add">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router';
import { ref,onMounted } from 'vue';
import axios from 'axios';
const routher = useRouter();

onMounted(()=>{
    dd()
})
const xiala=ref([{
    "memberClassId": 0,
    "memberName": ""
}])
const dd=()=>{
    axios.get("https://localhost:7295/api/MemberClass/GetMemberClasses",{params:xiala.value}).then(res=>{
        xiala.value=res.data;
    })
}

const add=()=>{
    if(data.value.name==""){
        alert("用户姓名不能为空")
        return;
    }
    if(data.value.phone==""){
        alert("手机后不能为空")
        return;
    }
    if(data.value.price<0){
        alert("消费金额不为0")
        return;
    }
    if(data.value.prices<0){
        alert("消费次数不为0")
        return;
    }
    if(data.value.initd<0){
        alert("可用积分不为0")
        return;
    }
    if(data.value.initds<0){
        alert("可用余额不为0")
        return;
    }

    axios.post("https://localhost:7295/api/Member/AddMember",data.value).then(res=>{
        if(res.data>0){
            alert("添加成功")
            routher.push({path:"/show"})
        }
        else{
            alert("添加失败")
        }
    })
}

const data=ref({
  "id": 0,
  "name": "",
  "sex": true,
  "phone": "",
  "memberClassId": 0,
  "price": 0,
  "prices": 0,
  "initd": 0,
  "initds": 0,
  "indele": true,
  //"createDates": ""
})
</script>

<style scoped>

</style>